package com.ruoyi.framework.shiro.token;

import org.apache.shiro.authc.UsernamePasswordToken;

import java.io.Serializable;

/**
 * 自定义token，用于兼容手机号+验证码登录
 */
public class UserNamePasswordAndMobileToken extends UsernamePasswordToken implements Serializable {
    /**
     *
     */
    private static final long serialVersionUID = 4812793519945855483L;

    // 手机号码
    private String mobileNum = null;

    /**
     * 重写getPrincipal方法
     */
    @Override
    public Object getPrincipal() {
        // 如果获取到用户名，则返回用户名，否则返回电话号码
        if (mobileNum == null) {
            return getUsername();
        } else {
            return getMobileNum();
        }
    }

    /**
     * 重写getCredentials方法
     */
    @Override
    public Object getCredentials() {
        // 如果获取到密码，则返回密码，否则返回null
        if (mobileNum == null) {
            return getPassword();
        } else {
            return "ok";
        }
    }

    public UserNamePasswordAndMobileToken() {
    }

    public UserNamePasswordAndMobileToken(final String mobileNum) {
        this.mobileNum = mobileNum;
    }

    public UserNamePasswordAndMobileToken(final String userName, final String password) {
        super(userName, password);
    }

    public UserNamePasswordAndMobileToken(final String userName, final String password, boolean rememberMe) {
        super(userName, password, rememberMe);
    }

    public UserNamePasswordAndMobileToken(final String mobileNum, final boolean rememberMe) {
        this.mobileNum = mobileNum;
        super.setRememberMe(rememberMe);
    }


    public String getMobileNum() {
        return mobileNum;
    }

    public void setMobileNum(String mobileNum) {
        this.mobileNum = mobileNum;
    }


    public static long getSerialversionuid() {
        return serialVersionUID;
    }

    @Override
    public String toString() {
        return "UserNamePasswordAndMobileToken [mobileNum=" + mobileNum + "]";
    }
}
